package com.example.im
import android.text.TextUtils
import cc.vv.lkimcomponent.lkim.bean.LKIMMessage
import cc.vv.lkimcomponent.lkim.bean.venum.LKIMChatType
import cc.vv.lkbasecomponent.util.LKJsonUtil
import cc.vv.lkbasecomponent.util.LKStringUtil
import cc.vv.lklibrary.log.LogOperate
import com.example.baselibrary.R
import com.example.baselibrary.bean.UserManager
import com.example.baselibrary.db.dao.AccountDao
import com.example.baselibrary.db.dao.GroupDao
import com.example.im.bean.IMCmdAction
import com.example.im.server.im.IMOtherService
import com.example.util.IntegerUtil
import com.example.util.LKImage
import com.example.util.finallist.*


/**
 * @author  chaohaipeng  on 2018/7/17.
 */
class IMMessageInit private constructor(){
        private object mHolder {
            val instance = IMMessageInit()
        }
        companion object {
            fun getInstance(): IMMessageInit {
                return mHolder.instance
            }
        }

    /**
     * 初始化文本类型消息
     *
     * @param account      帐号
     * @param content      消息内容
     * @param lkimChatType 单聊群聊    LKIMChatType
     * @return 消息体
     */
    fun initTxt(account: String, content: String, lkimChatType: LKIMChatType): LKIMMessage {
        var txtSendMessage = LKIMMessage.createTxtSendMessage(content, account)
        try {
            txtSendMessage = initBaseExt(account, txtSendMessage, lkimChatType)
            txtSendMessage.setAttribute(IMExtKey.EXTKEY_MESSAGETYPE, TEXT_TYPE)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return txtSendMessage
    }

    /**
     * 本地插入
     *
     * @param account     帐号
     * @param lkimMessage 消息体
     * @return 消息体
     */
    fun initSingleReceiveBaseExt(account: String, lkimMessage: LKIMMessage): LKIMMessage {
        lkimMessage.setAttribute(IMExtKey.EXTKEY_CONVERSATIONTYPE, SkKey.BTKEY_String_1)
        val accountTable = AccountDao.getInstance().queryById(account)
        if (null != accountTable) {
            lkimMessage.setAttribute(IMExtKey.EXTKEY_USERID, accountTable!!.account)
            lkimMessage.setAttribute(IMExtKey.EXTKEY_NICK, accountTable!!.nick)
            lkimMessage.setAttribute(IMExtKey.EXTKEY_AVATAR, accountTable!!.avatar)
            lkimMessage.setAttribute(IMExtKey.EXTKEY_POSITION, accountTable!!.position)
            lkimMessage.setAttribute(IMExtKey.EXTKEY_MEMBERID, accountTable!!.memberId)
        }
        return lkimMessage
    }
    /**
     * 初始化语音类型消息
     *
     * @param account      帐号
     * @param filePath     文件路径
     * @param duration     时间(秒)
     * @param lkimChatType 单聊群聊    LKIMChatType
     * @return 消息体
     */
    fun initVoice(account: String, filePath: String, duration: Int, lkimChatType: LKIMChatType): LKIMMessage {
        var voiceSendMessage = LKIMMessage.createVoiceSendMessage(filePath, duration, account)
        try {
            voiceSendMessage = initBaseExt(account, voiceSendMessage, lkimChatType)
            voiceSendMessage.setAttribute(IMExtKey.EXTKEY_MESSAGETYPE, VOICE_TYPE)
            voiceSendMessage.setAttribute(IMExtKey.EXTKEY_DURATION, duration.toString())
        } catch (e: Exception) {
            e.printStackTrace()
        }

        return voiceSendMessage
    }


    /**
     * 初始化图片类型消息
     *
     * @param account      帐号
     * @param imagePath    图片路径
     * @param lkimChatType 单聊群聊    LKIMChatType
     * @return 消息体
     */
    fun initImage(account: String, imagePath: String, lkimChatType: LKIMChatType): LKIMMessage {
        var imageSendMessage = LKIMMessage.createImageSendMessage(imagePath, account)
        try {
            val width = LKImage.getInstance().getImageWidth(imagePath)
            val height = LKImage.getInstance().getImageHeight(imagePath)
            imageSendMessage = initBaseExt(account, imageSendMessage, lkimChatType)
            imageSendMessage.setAttribute(IMExtKey.EXTKEY_MESSAGETYPE, IMAGE_TYPE)
            imageSendMessage.setAttribute(IMExtKey.EXTKEY_IMAGEWIDTH, width.toString())
            imageSendMessage.setAttribute(IMExtKey.EXTKEY_IMAGEHEIGHT, height.toString())
        } catch (e: Exception) {
            e.printStackTrace()
        }

        return imageSendMessage
    }

    /**
     * 初始化自定义表情类型消息
     *
     * @param account      帐号
     * @param imageUrl     图片地址
     * @param width        宽
     * @param height       高
     * @param lkimChatType 单聊群聊
     * @return 消息体
     */
    fun initCustomPhiz(account: String, imageUrl: String, width: String, height: String, lkimChatType: LKIMChatType): LKIMMessage {
        var customPhizSendMessage = LKIMMessage.createTxtSendMessage("[自定义表情]", account)
        try {
            customPhizSendMessage = initBaseExt(account, customPhizSendMessage, lkimChatType)
            customPhizSendMessage.setAttribute(IMExtKey.EXTKEY_MESSAGETYPE, EMOJ_TYPE)
//            customPhizSendMessage.setAttribute(IMExtKey.EXTKEY_IMAGEPATH, imageUrl)
//            customPhizSendMessage.setAttribute(IMExtKey.EXTKEY_URL, imageUrl)
            customPhizSendMessage.setAttribute(IMExtKey.EXTKEY_IMAGEWIDTH, width)
            customPhizSendMessage.setAttribute(IMExtKey.EXTKEY_IMAGEHEIGHT, height)
        } catch (e: Exception) {
            e.printStackTrace()
        }

        return customPhizSendMessage
    }

    /**
     * 初始化群组信息到扩展数据
     *
     * @param account
     * @param lkimMessage
     */
    fun initGroupInfoExt(account: String, lkimMessage: LKIMMessage) {
        val groupTable = GroupDao.getInstance().queryById(account)
        lkimMessage.setAttribute(IMExtKey.EXTKEY_GROUPID, account)

        var groupNick = LKStringUtil.getString(R.string.app_name)
        if (null != groupTable && !TextUtils.isEmpty(groupTable!!.groupNick)) {
            groupNick = groupTable!!.groupNick
        }
        lkimMessage.setAttribute(IMExtKey.EXTKEY_GROUPNICK, groupNick)

        var groupAvatar = ""
        if (null != groupTable && !TextUtils.isEmpty(groupTable!!.groupAvatar)) {
            groupAvatar = groupTable!!.groupAvatar
        }
        lkimMessage.setAttribute(IMExtKey.EXTKEY_GROUPAVATAR, groupAvatar)
        lkimMessage.setAttribute(IMExtKey.EXTKEY_READMEMBERSCOUNT, SkKey.BTKEY_String_0)

        var groupTotalCount = SkKey.BTKEY_String_0
        if (null != groupTable && IntegerUtil.getInstance().isInteger(groupTable!!.groupCount)) {
            groupTotalCount = (Integer.valueOf(groupTable?.groupCount) - 1).toString()
        }
        lkimMessage.setAttribute(IMExtKey.EXTKEY_GROUPMEMERCOUNT, groupTotalCount)
        val assembleMemberIds = IMOtherService.getInstance().assembleGroupMemberId(account)
        /*未阅读成员id*/
        lkimMessage.setAttribute(IMExtKey.EXTKEY_UNREADMEMBERIDS, assembleMemberIds)
        /*已阅读成员id*/
        lkimMessage.setAttribute(IMExtKey.EXTKEY_READMEMBERIDS, "")
    }
    /**
     * 初始化小视频
     *
     * @param account      帐号
     * @param videoUrl     视频路径
     * @param thumbUrl     视频缩略图地址
     * @param duration     时间(秒)
     * @param lkimChatType 单聊群聊    LKIMChatType
     * @return 消息体
     */
    fun initVideo(account: String, videoUrl: String, thumbUrl: String, duration: Int, lkimChatType: LKIMChatType): LKIMMessage {
        var videoSendMessage = LKIMMessage.createVideoSendMessage(videoUrl, thumbUrl, duration, account)
        try {
            videoSendMessage = initBaseExt(account, videoSendMessage, lkimChatType)
            videoSendMessage.setAttribute(IMExtKey.EXTKEY_MESSAGETYPE, VIDEO_TYPE)
        } catch (e: Exception) {
            e.printStackTrace()
        }

        return videoSendMessage
    }

    /**
     * 初始化群组变动透传
     *
     * @param groupId     群组id
     * @param groupNick   群组昵称
     * @param groupAvatar 群组头像
     * @param inform      提示内容
     * @return 消息体
     */
    fun initCMDGroupChange(groupId: String, groupNick: String, groupAvatar: String, inform: String, vararg toUserIds: String): LKIMMessage {
        val imCmdAction = initCMDBaseAction(groupId, LKIMChatType.GroupChat, SkKey.BTKEY_String_5)
        imCmdAction.groupId = groupId
        imCmdAction.groupNick = groupNick
        imCmdAction.groupAvatar = groupAvatar
        imCmdAction.tipMessage = inform
        if (null != toUserIds && toUserIds.size > 0) {
            val stringBuffer = StringBuffer()
            for (toUserId in toUserIds) {
                stringBuffer.append(toUserId + ",")
            }
            imCmdAction.toUserIds = stringBuffer.substring(0, stringBuffer.length - 1)
        }
        val action = LKJsonUtil.parseObjTJson(imCmdAction)

        return LKIMMessage.createCmdSendMessage(action, groupId)
    }

    /**
     * 所有类型消息必带扩展数据
     *
     * @param account      帐号
     * @param lkimMessage  消息体
     * @param lkimChatType 单聊群聊    LKIMChatType
     * @return 消息体
     */
    fun initBaseExt(account: String, lkimMessage: LKIMMessage, lkimChatType: LKIMChatType): LKIMMessage {
        if (LKIMChatType.SingleChat == lkimChatType) {
            lkimMessage.setAttribute(IMExtKey.EXTKEY_CONVERSATIONTYPE, OTHER_TYPE)
        } else if (LKIMChatType.GroupChat == lkimChatType) {
            lkimMessage.setAttribute(IMExtKey.EXTKEY_CONVERSATIONTYPE, TEXT_TYPE)
            initGroupInfoExt(account, lkimMessage)
        }
        LogOperate.e("头像======="+UserManager.userAvatar)
        lkimMessage.setAttribute(IMExtKey.EXTKEY_USERID, UserManager.userId)
        lkimMessage.setAttribute(IMExtKey.EXTKEY_NICK, UserManager.userNick)
        lkimMessage.setAttribute(IMExtKey.EXTKEY_AVATAR, UserManager.userAvatar)
        lkimMessage.setAttribute(IMExtKey.EXTKEY_POSITION, UserManager.position)
        lkimMessage.setAttribute(IMExtKey.EXTKEY_MEMBERID, UserManager.memberId)
        return lkimMessage
    }

    /**
     * 初始化消息已读透传
     *
     * @param account      帐号
     * @param messageId    消息id
     * @param lkimChatType 单聊群聊    LKIMChatType
     * @return 消息体
     */
    fun initCMDAck(account: String, messageId: String, lkimChatType: LKIMChatType): LKIMMessage {
        val imCmdAction = initCMDBaseAction(account, lkimChatType, SkKey.BTKEY_String_4)
        imCmdAction.messageId = messageId
        val action = LKJsonUtil.parseObjTJson(imCmdAction)
        return LKIMMessage.createCmdSendMessage(action, account)
    }

    /**
     * 初始化撤销透传消息体
     *
     * @param account      帐号
     * @param messageId    消息id
     * @param lkimChatType 单聊群聊    LKIMChatType
     * @return 消息体
     */
    fun initCMDRepeal(account: String, messageId: String, lkimChatType: LKIMChatType): LKIMMessage {
        val imCmdAction = initCMDBaseAction(account, lkimChatType, TEXT_TYPE)
        imCmdAction.messageId = messageId
        val action = LKJsonUtil.parseObjTJson(imCmdAction)
        return LKIMMessage.createCmdSendMessage(action, account)
    }

    /**
     * 初始化所有CMD类型消息必带扩展
     *
     * @param account      帐号
     * @param lkimChatType 单聊群聊    LKIMChatType
     * @param messageType  cmd消息类型
     * @return 消息体
     */
    fun initCMDBaseAction(account: String, lkimChatType: LKIMChatType, messageType: String): IMCmdAction {
        val imCmdAction = IMCmdAction()
        if (LKIMChatType.SingleChat == lkimChatType) {
            imCmdAction.conversationType = OTHER_TYPE
            imCmdAction.conversationId = UserManager.userId
        } else if (LKIMChatType.GroupChat == lkimChatType) {
            imCmdAction.conversationType = TEXT_TYPE
            imCmdAction.conversationId = account
        }
        imCmdAction.userId = UserManager.userId
        imCmdAction.nick = UserManager.userNick
        imCmdAction.avatar = UserManager.userAvatar
        imCmdAction.messageType = messageType
        return imCmdAction
    }

}